package com.web.hotel.dao;

import com.web.hotel.po.Dictionary;
import com.web.hotel.service.DictionaryService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service(value="dictionaryService")
@Transactional(rollbackFor = { Exception.class })
public class DictionaryDAO implements DictionaryService{

    @Resource
    SessionFactory factory;

    public List<Dictionary> getDictionaryByDicName(String dicName) {
        List<Dictionary> dictionaryList = new ArrayList<Dictionary>();
        Session s = null;
        StringBuffer querySQL = new StringBuffer();
        StringBuffer whereSQL = new StringBuffer();
        if(dicName != null && !dicName.equals("")){
            whereSQL.append(" and dicName = '"+dicName+"'");
        }
        querySQL.append("from Dictionary c");
        querySQL.append(" where 1 = 1");
        if(whereSQL.length() > 0){
            querySQL.append(whereSQL);
        }
        querySQL.append(" order by dicId asc");
        try {
            s = factory.getCurrentSession();
            Query query = s.createQuery(querySQL.toString());
            dictionaryList = query.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dictionaryList;
    }
}
